//    This file is part of Open WordSearch.
//
//    Open WordSearch is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Open WordSearch is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Open WordSearch.  If not, see <http://www.gnu.org/licenses/>.
//
//        Copyright 2009, 2010 Brendan Dahl <dahl.brendan@brendandahl.com>
//              http://www.brendandahl.com
package com.abhipedro.wordsearch.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;

public class SettingsActivity extends PreferenceActivity  {
	// Option names and default values
	private static final String OPT_MUSIC = "music";
	private static final String OPT_VIBRATE = "vibrate";

	private static final String OPT_THEME = "theme";
	private static final String OPT_THEME_DEF = "2";
	
	private  String RATE_MARKET =  "market://details?id=com.abhipedro.wordsearch.view";


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);
		
		Preference aboutPref = (Preference) findPreference("Feedback");
		aboutPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				feedback();
				return true;
			}
		});
		
		Preference sharePref = (Preference) findPreference("Share");
		sharePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				shareApp();
				return true;
			}
		});
		
		Preference ratePref = (Preference) findPreference("Rate");
		ratePref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				rateApp();
				return true;
			}
		});
	}
	
	
	private void feedback() {
		String title = "Word Search - Feedback";
		String text = "";
		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		String[] recipients = new String[] {"mobilenewsdev@gmail.com" };
		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
		emailIntent.setType("message/rfc822");
		startActivity(Intent.createChooser(emailIntent, "Feedback"));
	}
	
	private void shareApp() {
		String title = "Word Search";
		String text = "Check out Wordsearch - the best word game for your Android phone : " +
				"https://market.android.com/details?id=com.abhipedro.wordsearch.view";
		Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
		String[] recipients = new String[] { "", };
		emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
		emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
		emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
		emailIntent.setType("text/plain");
		startActivity(Intent.createChooser(emailIntent, "Share"));
	}
	
	private void rateApp() {
		Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri
				.parse(RATE_MARKET));
		startActivity(marketIntent);
	}

	/** Get the current value of the music option */
	public static boolean getMusic(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean(OPT_MUSIC, false);
	}

	/** Get the current value of the hints option */
	public static boolean getVibration(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getBoolean(OPT_VIBRATE, true);
	}

	/**
	 * Get the current value of Theme for the game
	 **/
	public static String getTheme(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context)
				.getString(OPT_THEME, OPT_THEME_DEF);
	}

}
